package cn.wzut.edut.controller;

import cn.wzut.edut.dao.pojo.table.PtyStuGroup;
import cn.wzut.edut.pojo.*;
import cn.wzut.edut.service.StuGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/stu_group")
public class StuGroupController {

    @Autowired
    StuGroupService stuGroupService;

    /**
     * 获取全部学生组列表
     *
     * @return
     */
    @PostMapping("get_all")
    public JsonResult getAllGroupList(HttpServletRequest request){
        String token = request.getHeader("token");
        List<PtyStuGroup> groupList = stuGroupService.getAllGroupList(token);
        if(groupList != null){
            return new JsonResult(groupList);
        }
        return new JsonResult(-1, "获取学生组列表失败");
    }

    /**
     * 获取教师所拥有学生组列表
     *
     * @return
     */
    @PostMapping("get_own_all")
    public JsonResult getGroupList(HttpServletRequest request){
        String token = request.getHeader("token");
        List<PtyStuGroup> groupList = stuGroupService.getGroupList(token);
        if(groupList != null){
            return new JsonResult(groupList);
        }
        return new JsonResult(-1, "获取学生组列表失败");
    }

    /**
     * 新增学生组
     * @return
     */
    @PostMapping("add")
    public JsonResult addGroup(HttpServletRequest request, StuGroupInfo stuGroupInfo){
        String token = request.getHeader("token");
        if(stuGroupService.addGroup(token, stuGroupInfo)){
            return new JsonResult(1, "添加学生组成功");
        }
        return new JsonResult(-1, "添加学生组失败");
    }

    /**
     * 删除学生组
     * @return
     */
    @PostMapping("del")
    public JsonResult DelGroup(HttpServletRequest request, String[] groupIds){
        String token = request.getHeader("token");
        if(stuGroupService.delGroup(token, groupIds)){
            return new JsonResult(1, "删除学生组成功");
        }
        return new JsonResult(-1, "删除学生组失败");
    }

    /**
     * 修改学生组
     * @return
     */
    @PostMapping("mod")
    public JsonResult modGroup(HttpServletRequest request, StuGroupModInfo stuGroupModInfo){
        String token = request.getHeader("token");
        if(stuGroupService.modGroup(token, stuGroupModInfo)){
            return new JsonResult(1, "修改学生组成功");
        }
        return new JsonResult(-1, "修改学生组失败");
    }

    /**
     * 获取学生组信息
     * @return
     */
    @PostMapping("get")
    public JsonResult getCollegeInf(HttpServletRequest request, String groupId){
        String token = request.getHeader("token");
        StuGroupInfo stuGroupInfo = stuGroupService.getGroupById(token, groupId);
        if(stuGroupInfo != null){
            return new JsonResult(stuGroupInfo);
        }
        return new JsonResult(-1, "获取学生组信息失败");
    }

}
